package com.amsp.oa.oademo.controller;

import com.amsp.oa.oademo.entity.EnvironmentApply;
import com.amsp.oa.oademo.service.EnvironmentApplyService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/environment")
public class EnvironmentApplyController {

    private static final Logger log = LoggerFactory.getLogger(EnvironmentApplyController.class);

    @Autowired
    private EnvironmentApplyService environmentApplyService;

    @GetMapping("/list")
    public ResponseEntity<IPage<EnvironmentApply>> getApplyList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String type,
            @RequestParam(required = false) String status) {
        Page<EnvironmentApply> pageParam = new Page<>(page, size);
        return ResponseEntity.ok(environmentApplyService.getApplyList(pageParam, keyword, type, status));
    }

    @GetMapping("/my-applications")
    public ResponseEntity<IPage<EnvironmentApply>> getMyApplies(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam String applicant,
            @RequestParam(required = false) String type) {
        Page<EnvironmentApply> pageParam = new Page<>(page, size);
        return ResponseEntity.ok(environmentApplyService.getMyApplies(pageParam, applicant, type));
    }

    @GetMapping("/{type}/detail/{id}")
    public ResponseEntity<EnvironmentApply> getApply(
            @PathVariable String type,
            @PathVariable Long id) {
        return ResponseEntity.ok(environmentApplyService.getById(id));
    }

    @PostMapping("/{type}/submit")
    public ResponseEntity<EnvironmentApply> submitApply(
            @PathVariable String type,
            @RequestBody EnvironmentApply apply) {
        try {
            log.info("Received submit request for type: {}, data: {}", type, apply);
            apply.setType(type);
            EnvironmentApply result = environmentApplyService.submitApply(apply);
            log.info("Submit successful, result: {}", result);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            log.error("Submit failed", e);
            throw e;
        }
    }

    @PutMapping("/{type}/{id}")
    public ResponseEntity<EnvironmentApply> updateApply(
            @PathVariable String type,
            @PathVariable Long id,
            @RequestBody EnvironmentApply apply) {
        apply.setId(id);
        apply.setType(type);
        return ResponseEntity.ok(environmentApplyService.updateApply(apply));
    }

    @DeleteMapping("/{type}/{id}")
    public ResponseEntity<Void> deleteApply(
            @PathVariable String type,
            @PathVariable Long id) {
        environmentApplyService.deleteApply(id);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/{type}/handle/{id}")
    public ResponseEntity<EnvironmentApply> handleApply(
            @PathVariable String type,
            @PathVariable Long id,
            @RequestParam String handler,
            @RequestParam String handleResult) {
        return ResponseEntity.ok(environmentApplyService.handleApply(id, handler, handleResult));
    }

    @PostMapping("/{type}/cancel/{id}")
    public ResponseEntity<EnvironmentApply> cancelApply(
            @PathVariable String type,
            @PathVariable Long id) {
        return ResponseEntity.ok(environmentApplyService.cancelApply(id));
    }
} 